#!/bin/bash
#-------------------------------------------------------------------------------
# Installation return codes:
#
# 0 - no error
# 1 - usage, input argument incorrect
# 2 - unable to mount service media
# 3 - error unzip'ing file on disk
# 4 - .signature file missing from service package
# 5 - service install script missing
# 6 - incompatible service release
# 7 - incorrect file successfully downloaded to system - better not happen!
# 8* - error executing install script, return the script error instead
#-------------------------------------------------------------------------------

#-------------------------------------------------------------------------------
# Initialize service script return code
#-------------------------------------------------------------------------------
CDROM_DIR=/media/cdrom
INSTALL_DIR=/usr/local/hsc_install.images

MD5FILE=/tmp/md5.$$
RESULT=/tmp/res.$$
CNFILE=/tmp/cn.$$

OPENSSL=/usr/bin/openssl
MD5SUM=/usr/bin/md5sum
VERIFY=/opt/hsc/sbin/verify
DIFF=/usr/bin/diff
FIND=/usr/bin/find
ECHO=/bin/echo
SORT=/bin/sort
CAT=/bin/cat
CUT=/usr/bin/cut
RM=/bin/rm

#-------------------------------------------------------------------------------
# Common exit point
#-------------------------------------------------------------------------------
exit_cleanup() {
   cd /
   umount $CDROM_DIR > /dev/null 2>&1
   rm -rf $INSTALL_DIR > /dev/null 2>&1
   mkdir -p $INSTALL_DIR > /dev/null 2>&1
   chmod 777 $INSTALL_DIR > /dev/null 2>&1

   exit $1
}

InstallUpdates() {
   if [ -f $IMG_BASE/images/.signature ]; then
       typeset -i cver
       typeset -i dver
       cver=`/opt/hsc/bin/hsc version | grep Version | $CUT -d':' -f 2`
       dver=`$CAT $IMG_BASE/images/.signature | $CUT -d' ' -f 2`
       if [ $cver -eq $dver ]; then
           if [ -x $IMG_BASE/images/installImages ]; then
               $IMG_BASE/images/installImages $IMG_BASE/images
               serviceRC=$?
               if [ $serviceRC -ne 0 ]; then
                   exit_cleanup 6
               fi
           else
               echo "Install script not found" 1>&2
               exit_cleanup 7
           fi
       else
           echo "Incompatible service package" 1>&2
           exit_cleanup 8
       fi
   else
       echo "Missing Signature file" 1>&2
       exit_cleanup 9
   fi
}


if [ "$*" = "" ]; then
    echo "Usage: applyUpdates [cdrom | disk]"
    exit_cleanup 1
fi

MEDIA=$1
IMG_BASE=$CDROM_DIR

case $MEDIA in
  cdrom )
    MOUNTED=`mount | grep cdrom | wc -l`
    if [ $MOUNTED = '0' ]; then
        mount $CDROM_DIR
        if [ $? -ne 0 ]; then
            echo "Unable to mount media" 1>&2
            exit_cleanup 2
        fi
    fi
    ;;


  disk )
    IMG_BASE=$INSTALL_DIR
    /usr/bin/unzip -o $IMG_BASE/*.zip -d $IMG_BASE
    if [ $? -ne 0 ]; then
        echo "Error uncompressing package" 1>&2
        exit_cleanup 3
    fi
    ;;

  *)
    IMG_BASE=$INSTALL_DIR
    /usr/bin/unzip -o $MEDIA -d $IMG_BASE
    if [ $? -ne 0 ]; then
        echo "Error uncompressing package" 1>&2
        exit_cleanup 3
    fi
    ;;

esac


echo "Verifying Certificate Information"
/opt/hsc/sbin/AuthImage $IMG_BASE
if [ $? -eq 0 ]; then
    $RM -f $MD5FILE
    cd $IMG_BASE
    echo "Authenticating Install Packages"
    $FIND images -type f -exec $MD5SUM {} \; >> $MD5FILE
    $SORT $MD5FILE | $MD5SUM | $CUT -f1 -d " " > $RESULT

    $OPENSSL x509 -subject -noout -in cert.pem |$CUT -f8 -d "=" > $CNFILE
    $DIFF $RESULT $CNFILE > /dev/null 2>&1

    if  [ $? -eq 0 ]; then
        echo "Installing Packages"
        InstallUpdates
    else
        $ECHO "Install package corruption. HMC not updated"
        exit_cleanup 4
    fi
else
    $ECHO "Authentication Failure. HMC not updated"
    $ECHO "Corrupted install image or incorrect time on HMC"
    $ECHO "Please verify the date/time of the hmc and retry"
    exit_cleanup 5
fi

rm -f $MD5FILE $RESULT $CNFILE

exit_cleanup 0
